<!DOCTYPE html>
<html lang="zh-CN">
  <head>
  <meta charset="UTF-8">
  <meta 
    name="viewport"
    content="width=device-width, initial-scale=1.0, minimum-scale=1.0">
  <meta 
    http-equiv="X-UA-Compatible" 
    content="ie=edge">
  <meta 
    name="theme-color" 
    content="#fff" 
    id="theme-color">
  <meta 
    name="description" 
    content="hexoDemo用于介绍如何使用hexo">
  <link 
    rel="icon" 
    href="/">
  <title>17-内置对象：Date</title>
  
    
      <meta 
        property="og:title" 
        content="17-内置对象：Date">
    
    
      <meta 
        property="og:url" 
        content="https://zwl5670.gitee.io/2004/03/01/04-JavaScript%E5%9F%BA%E7%A1%80/17-%E5%86%85%E7%BD%AE%E5%AF%B9%E8%B1%A1%EF%BC%9ADate/index.html">
    
    
      <meta 
        property="og:img" 
        content="https://img.songhn.com/img/Y67gdd.png">
    
    
      <meta 
        property="og:img" 
        content="hexoDemo用于介绍如何使用hexo">
    
    
      <meta 
        property="og:type" 
        content="article">
      <meta 
        property="og:article:published_time" 
        content="2004-03-01">
      <meta 
        property="og:article:modified_time" 
        content="2023-03-14">
      <meta 
        property="og:article:author" 
        content="zwl5670">
      
        
          <meta 
            property="og:article:tag" 
            content="JS基础">
        
      
    
  
  
  <link rel="preload" href="//at.alicdn.com/t/font_1946621_i1kgafibvw.css" as="style" >
  <link rel="preload" href="//at.alicdn.com/t/font_1952792_89b4ac4k4up.css" as="style" >
  <link rel="preload" href="/css/main.css" as="style" >
  
  <link rel="modulepreload" href="//instant.page/5.1.0">
  
  <link rel="stylesheet" href="/css/main.css">
  
  <link rel="stylesheet" href="//at.alicdn.com/t/font_1946621_i1kgafibvw.css">
  
  <link rel="stylesheet" href="//at.alicdn.com/t/font_1952792_89b4ac4k4up.css">
  
  
  
    <link rel="stylesheet" href="/js/lib/lightbox/baguetteBox.min.css">
  
  <script>
    function loadScript(url, cb) {
      var script = document.createElement('script');
      script.src = url;
      if (cb) script.onload = cb;
      script.async = true;
      document.body.appendChild(script);
    }
    function loadCSS(href, data, attr) {
      var sheet = document.createElement('link');
      sheet.ref = 'stylesheet';
      sheet.href = href;
      sheet.dataset[data] = attr;
      document.head.appendChild(sheet);
    }
    function changeCSS(cssFile, data, attr) {
      var oldlink = document.querySelector(data);
      var newlink = document.createElement("link");
      newlink.setAttribute("rel", "stylesheet");
      newlink.setAttribute("href", cssFile);
      newlink.dataset.prism = attr;
      document.head.replaceChild(newlink, oldlink);
    }
  </script>
  
    
  
  <script>
    // control reverse button
    var reverseDarkList = {
      dark: 'light',
      light: 'dark'
    };
    var themeColor = {
      dark: '#1c1c1e',
      light: '#fff'
    }
    // get the data of css prefers-color-scheme
    var getCssMediaQuery = function() {
      return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
    };
    // reverse current darkmode setting function
    var reverseDarkModeSetting = function() {
      var setting = localStorage.getItem('user-color-scheme');
      if(reverseDarkList[setting]) {
        setting = reverseDarkList[setting];
      } else if(setting === null) {
        setting = reverseDarkList[getCssMediaQuery()];
      } else {
        return;
      }
      localStorage.setItem('user-color-scheme', setting);
      return setting;
    };
    // apply current darkmode setting
  </script>
  
    <script>
      var setDarkmode = function(mode) {
      var setting = mode || localStorage.getItem('user-color-scheme');
      if(setting === getCssMediaQuery()) {
        document.documentElement.removeAttribute('data-user-color-scheme');
        localStorage.removeItem('user-color-scheme');
        document.getElementById('theme-color').content = themeColor[setting];
        document.getElementById('theme-color').dataset.mode = setting;
      } else if(reverseDarkList[setting]) {
        document.documentElement.setAttribute('data-user-color-scheme', setting);
        document.getElementById('theme-color').content = themeColor[setting];
        document.getElementById('theme-color').dataset.mode = setting;
      } else {
        document.documentElement.removeAttribute('data-user-color-scheme');
        localStorage.removeItem('user-color-scheme');
        document.getElementById('theme-color').content = themeColor[getCssMediaQuery()];
        document.getElementById('theme-color').dataset.mode = getCssMediaQuery();
      }
    };
    setDarkmode();
    </script>
  
  
  
    <link rel="preload" href="/js/lib/lightbox/baguetteBox.min.js" as="script">
    <link rel="preload" href="/js/lib/lightbox/baguetteBox.min.css" as="style" >
  
  
    <link rel="preload" href="/js/lib/lozad.min.js" as="script">
  
  
  
  
  
  
<meta name="generator" content="Hexo 6.3.0"></head>

  <body>
    <div class="wrapper">
       
      <nav class="navbar">
  <div class="navbar-logo">
    <a class="navbar-logo-main" href="/">
      
      <span class="navbar-logo-dsc">前端面试</span>
      </a>
  </div>
  <div class="navbar-menu">
    
      <a 
        href="/" 
        class="navbar-menu-item">
        
          首页
        
      </a>
    
      <a 
        href="/archives" 
        class="navbar-menu-item">
        
          归档
        
      </a>
    
      <a 
        href="/tags" 
        class="navbar-menu-item">
        
          标签
        
      </a>
    
      <a 
        href="/categories" 
        class="navbar-menu-item">
        
          分类
        
      </a>
    
      <a 
        href="/about" 
        class="navbar-menu-item">
        
          关于
        
      </a>
    
      <a 
        href="/links" 
        class="navbar-menu-item">
        
          友链
        
      </a>
    
    <button 
      class="navbar-menu-item darknavbar navbar-menu-btn" 
      aria-label="Toggle dark mode"
      id="dark">
      <i class="iconfont icon-weather"></i>
    </button>
    <button 
      class="navbar-menu-item searchnavbar navbar-menu-btn" 
      aria-label="Toggle search"
      id="search">
      <!-- <i 
        class="iconfont icon-search" 
        style="font-size: 1.2rem; font-weight: 400;">
      </i> -->
      <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img"
        class="iconify iconify--ion" width="28" height="28" preserveAspectRatio="xMidYMid meet" viewBox="0 0 512 512">
        <path fill="none" stroke="currentColor" stroke-miterlimit="10" stroke-width="28"
          d="M256 80a176 176 0 1 0 176 176A176 176 0 0 0 256 80Z"></path>
        <path fill="none" stroke="currentColor" stroke-miterlimit="10" stroke-width="28"
          d="M232 160a72 72 0 1 0 72 72a72 72 0 0 0-72-72Z"></path>
        <path fill="none" stroke="currentColor" stroke-linecap="round" stroke-miterlimit="10" stroke-width="28"
          d="M283.64 283.64L336 336"></path>
      </svg>
    </button>
  </div>
</nav> 
      
      <div 
        id="local-search" 
        style="display: none">
        <input
          class="navbar-menu-item"
          id="search-input"
          placeholder="请输入搜索内容..." />
        <div id="search-content"></div>
      </div>
      
      <div class="section-wrap">
        <div class="container">
          <div class="columns">
            <aside class="left-column">
              
              <div class="card card-author">
                
  <img 
    src="https://img.songhn.com/img/Y67gdd.png" 
    class="author-img"
    width="88"
    height="88"
    alt="author avatar">

<p class="author-name">zwl5670</p>
<p class="author-description">前端学习资料</p>
<div class="author-message">
  <a 
    class="author-posts-count" 
    href="/archives">
    <span>215</span>
    <span>文章</span>
  </a>
  <a 
    class="author-categories-count" 
    href="/categories">
    <span>14</span>
    <span>分类</span>
  </a>
  <a 
    class="author-tags-count" 
    href="/tags">
    <span>14</span>
    <span>标签</span>
  </a>
</div>

              </div>
               <div class="sticky-tablet">
  
  
    <article class="display-when-two-columns spacer">
      <div class="card card-content toc-card">
        <div class="toc-header">
  <i 
    class="iconfont icon-menu" 
    style="padding-right: 2px;">
  </i>目录
</div>
<ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%86%85%E7%BD%AE%E5%AF%B9%E8%B1%A1%EF%BC%9ADate"><span class="toc-text">内置对象：Date</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%88%9B%E5%BB%BADate%E5%AF%B9%E8%B1%A1"><span class="toc-text">创建Date对象</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%86%99%E6%B3%95%E4%B8%80%EF%BC%9A%E4%B8%8D%E4%BC%A0%E9%80%92%E5%8F%82%E6%95%B0%E6%97%B6%EF%BC%8C%E5%88%99%E8%8E%B7%E5%8F%96%E7%B3%BB%E7%BB%9F%E7%9A%84%E5%BD%93%E5%89%8D%E6%97%B6%E9%97%B4%E5%AF%B9%E8%B1%A1"><span class="toc-text">写法一：不传递参数时，则获取系统的当前时间对象</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%86%99%E6%B3%95%E4%BA%8C%EF%BC%9A%E4%BC%A0%E9%80%92%E5%8F%82%E6%95%B0"><span class="toc-text">写法二：传递参数</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%97%A5%E6%9C%9F%E7%9A%84%E6%A0%BC%E5%BC%8F%E5%8C%96"><span class="toc-text">日期的格式化</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#Date%E5%AF%B9%E8%B1%A1%E7%9A%84%E6%96%B9%E6%B3%95"><span class="toc-text">Date对象的方法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%B8%BE%E4%BE%8B%EF%BC%9A%E5%B9%B4%E6%9C%88%E6%97%A5%E7%9A%84%E6%A0%BC%E5%BC%8F%E5%8C%96"><span class="toc-text">举例：年月日的格式化</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%8E%B7%E5%8F%96%E6%97%B6%E9%97%B4%E6%88%B3"><span class="toc-text">获取时间戳</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%97%B6%E9%97%B4%E6%88%B3%E7%9A%84%E5%AE%9A%E4%B9%89%E5%92%8C%E4%BD%9C%E7%94%A8"><span class="toc-text">时间戳的定义和作用</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#getTime-%EF%BC%9A%E8%8E%B7%E5%8F%96%E6%97%B6%E9%97%B4%E6%88%B3"><span class="toc-text">getTime()：获取时间戳</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%8E%B7%E5%8F%96-Date-%E5%AF%B9%E8%B1%A1%E7%9A%84%E6%97%B6%E9%97%B4%E6%88%B3"><span class="toc-text">获取 Date 对象的时间戳</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%8E%B7%E5%8F%96%E5%BD%93%E5%89%8D%E6%97%B6%E9%97%B4%E7%9A%84%E6%97%B6%E9%97%B4%E6%88%B3"><span class="toc-text">获取当前时间的时间戳</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%88%A9%E7%94%A8%E6%97%B6%E9%97%B4%E6%88%B3%E6%A3%80%E6%B5%8B%E4%BB%A3%E7%A0%81%E7%9A%84%E6%89%A7%E8%A1%8C%E6%97%B6%E9%97%B4"><span class="toc-text">利用时间戳检测代码的执行时间</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#format"><span class="toc-text">format()</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%BB%83%E4%B9%A0"><span class="toc-text">练习</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%B8%BE%E4%BE%8B1%EF%BC%9A%E6%A8%A1%E6%8B%9F%E6%97%A5%E5%8E%86"><span class="toc-text">举例1：模拟日历</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%B8%BE%E4%BE%8B2%EF%BC%9A%E5%8F%91%E5%B8%83%E4%BC%9A%E5%80%92%E8%AE%A1%E6%97%B6"><span class="toc-text">举例2：发布会倒计时</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Moment-js"><span class="toc-text">Moment.js</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%B5%9E%E8%B5%8F%E4%BD%9C%E8%80%85"><span class="toc-text">赞赏作者</span></a></li></ol>
      </div>
    </article>
  
  
  <article class="card card-content categories-widget">
    <div class="categories-card">
  <div class="categories-header">
    <i 
      class="iconfont icon-fenlei" 
      style="padding-right: 2px;">
    </i>分类
  </div>
  <div class="categories-list">
    
      <a href="/categories/%E5%89%8D%E7%AB%AF%E5%B7%A5%E5%85%B7/">
        <div class="categories-list-item">
          前端工具
          <span class="categories-list-item-badge">13</span>
        </div>
      </a>
    
      <a href="/categories/CSS%E5%9F%BA%E7%A1%80/">
        <div class="categories-list-item">
          CSS基础
          <span class="categories-list-item-badge">18</span>
        </div>
      </a>
    
      <a href="/categories/CSS%E8%BF%9B%E9%98%B6/">
        <div class="categories-list-item">
          CSS进阶
          <span class="categories-list-item-badge">9</span>
        </div>
      </a>
    
      <a href="/categories/HTML/">
        <div class="categories-list-item">
          HTML
          <span class="categories-list-item-badge">12</span>
        </div>
      </a>
    
      <a href="/categories/JS%E5%9F%BA%E7%A1%80/">
        <div class="categories-list-item">
          JS基础
          <span class="categories-list-item-badge">56</span>
        </div>
      </a>
    
      <a href="/categories/JS%E5%9F%BA%E7%A1%80ES6/">
        <div class="categories-list-item">
          JS基础ES6
          <span class="categories-list-item-badge">10</span>
        </div>
      </a>
    
      <a href="/categories/JS%E5%9F%BA%E7%A1%80%E5%BC%82%E6%AD%A5/">
        <div class="categories-list-item">
          JS基础异步
          <span class="categories-list-item-badge">11</span>
        </div>
      </a>
    
      <a href="/categories/JS%E8%BF%9B%E9%98%B6/">
        <div class="categories-list-item">
          JS进阶
          <span class="categories-list-item-badge">11</span>
        </div>
      </a>
    
      <a href="/categories/%E7%A7%BB%E5%8A%A8web/">
        <div class="categories-list-item">
          移动web
          <span class="categories-list-item-badge">3</span>
        </div>
      </a>
    
      <a href="/categories/CSS%E5%92%8CDOM/">
        <div class="categories-list-item">
          CSS和DOM
          <span class="categories-list-item-badge">6</span>
        </div>
      </a>
    
      <a href="/categories/Node/">
        <div class="categories-list-item">
          Node
          <span class="categories-list-item-badge">16</span>
        </div>
      </a>
    
      <a href="/categories/Vue%E5%9F%BA%E7%A1%80/">
        <div class="categories-list-item">
          Vue基础
          <span class="categories-list-item-badge">15</span>
        </div>
      </a>
    
      <a href="/categories/%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96/">
        <div class="categories-list-item">
          性能优化
          <span class="categories-list-item-badge">7</span>
        </div>
      </a>
    
      <a href="/categories/%E5%89%8D%E7%AB%AF%E5%B7%A5%E7%A8%8B%E5%8C%96/">
        <div class="categories-list-item">
          前端工程化
          <span class="categories-list-item-badge">8</span>
        </div>
      </a>
    
  </div>
</div>
  </article>
  
  <article class="card card-content tags-widget">
    <div class="tags-card">
  <div class="tags-header">
    <i 
      class="iconfont icon-biaoqian" 
      style="padding-right: 2px;">
    </i>热门标签
  </div>
  <div class="tags-list">
    
      <a 
        href="/tags/JS%E5%9F%BA%E7%A1%80/" 
        title="JS基础">
        <div class="tags-list-item">JS基础</div>
      </a>
    
      <a 
        href="/tags/CSS%E5%9F%BA%E7%A1%80/" 
        title="CSS基础">
        <div class="tags-list-item">CSS基础</div>
      </a>
    
      <a 
        href="/tags/Node/" 
        title="Node">
        <div class="tags-list-item">Node</div>
      </a>
    
      <a 
        href="/tags/Vue%E5%9F%BA%E7%A1%80/" 
        title="Vue基础">
        <div class="tags-list-item">Vue基础</div>
      </a>
    
      <a 
        href="/tags/%E5%89%8D%E7%AB%AF%E5%B7%A5%E5%85%B7/" 
        title="前端工具">
        <div class="tags-list-item">前端工具</div>
      </a>
    
      <a 
        href="/tags/HTML/" 
        title="HTML">
        <div class="tags-list-item">HTML</div>
      </a>
    
      <a 
        href="/tags/JS%E8%BF%9B%E9%98%B6/" 
        title="JS进阶">
        <div class="tags-list-item">JS进阶</div>
      </a>
    
      <a 
        href="/tags/JS%E5%9F%BA%E7%A1%80%E5%BC%82%E6%AD%A5/" 
        title="JS基础异步">
        <div class="tags-list-item">JS基础异步</div>
      </a>
    
      <a 
        href="/tags/JS%E5%9F%BA%E7%A1%80ES6/" 
        title="JS基础ES6">
        <div class="tags-list-item">JS基础ES6</div>
      </a>
    
      <a 
        href="/tags/CSS%E8%BF%9B%E9%98%B6/" 
        title="CSS进阶">
        <div class="tags-list-item">CSS进阶</div>
      </a>
    
      <a 
        href="/tags/%E5%89%8D%E7%AB%AF%E5%B7%A5%E7%A8%8B%E5%8C%96/" 
        title="前端工程化">
        <div class="tags-list-item">前端工程化</div>
      </a>
    
      <a 
        href="/tags/%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96/" 
        title="性能优化">
        <div class="tags-list-item">性能优化</div>
      </a>
    
      <a 
        href="/tags/CSS%E5%92%8CDOM/" 
        title="CSS和DOM">
        <div class="tags-list-item">CSS和DOM</div>
      </a>
    
      <a 
        href="/tags/%E7%A7%BB%E5%8A%A8web/" 
        title="移动web">
        <div class="tags-list-item">移动web</div>
      </a>
    
  </div>
</div>
  </article>
  
  
</div>
            </aside>
            <main class="main-column">
              
<article class="card card-content">
  <header>
    <h1 class="post-title">
      17-内置对象：Date
    </h1>
  </header>
  <div class="post-meta post-show-meta">
    <time datetime="2004-02-29T16:00:00.000Z">
      <i 
        class="iconfont icon-calendar" 
        style="margin-right: 2px;">
      </i>
      <span>2004-03-01</span>
    </time>
    
      <span class="dot"></span>
      
        <a 
          href="/categories/JS%E5%9F%BA%E7%A1%80/" 
          class="post-meta-link">
          JS基础
        </a>
      
    
    
      <span class="dot"></span>
      <span>3.2k 字</span>
    
  </div>
  
    <div 
      class="post-meta post-show-meta" 
      style="margin-top: -10px;">
      <div style="display: flex; align-items: center;">
        <i 
          class="iconfont icon-biaoqian" 
          style="margin-right: 2px; font-size: 1.15rem;">
        </i>
        
          
          <a 
            href="/tags/JS%E5%9F%BA%E7%A1%80/" 
            class="post-meta-link">
            JS基础
          </a>
        
      </div>
    </div>
  
  </header>
  <div 
    id="section" 
    class="post-content">
    <p><ArticleTopAd></ArticleTopAd></p>
<h2 id="内置对象：Date"><a href="#内置对象：Date" class="headerlink" title="内置对象：Date"></a>内置对象：Date</h2><blockquote>
<p>Date 对象在实际开发中，使用得很频繁，且容易在细节地方出错，需要引起重视。</p>
</blockquote>
<p>内置对象 Date 用来处理日期和时间。</p>
<p><strong>需要注意的是</strong>：与 Math 对象不同，Date 对象是一个<strong>构造函数</strong> ，需要<strong>先实例化</strong>后才能使用。</p>
<h2 id="创建Date对象"><a href="#创建Date对象" class="headerlink" title="创建Date对象"></a>创建Date对象</h2><p>创建Date对象有两种写法：</p>
<ul>
<li><p>写法一：如果Date()不写参数，就返回当前时间对象</p>
</li>
<li><p>写法二：如果Date()里面写参数，就返回括号里输入的时间对象</p>
</li>
</ul>
<p>针对这两种写法，我们来具体讲一讲。</p>
<h3 id="写法一：不传递参数时，则获取系统的当前时间对象"><a href="#写法一：不传递参数时，则获取系统的当前时间对象" class="headerlink" title="写法一：不传递参数时，则获取系统的当前时间对象"></a>写法一：不传递参数时，则获取系统的当前时间对象</h3><p>代码举例：</p>
<pre class="highlight"><span class="line"><span class="keyword">var</span> date1 = <span class="keyword">new</span> <span class="title class_">Date</span>();</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(date1);</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="keyword">typeof</span> date1);</span><br></pre>

<p>代码解释：不传递参数时，表示的是获取系统的当前时间对象。也可以理解成是：获取当前代码执行的时间。</p>
<p>打印结果：</p>
<pre class="highlight"><span class="line">Mon Feb 17 2020 21:57:22 GMT+0800 (中国标准时间)</span><br><span class="line">object</span><br></pre>

<h3 id="写法二：传递参数"><a href="#写法二：传递参数" class="headerlink" title="写法二：传递参数"></a>写法二：传递参数</h3><p>传递参数时，表示获取指定时间的时间对象。参数中既可以传递字符串，也可以传递数字，也可以传递时间戳。</p>
<p>通过传参的这种写法，我们可以把时间字符串&#x2F;时间数字&#x2F;时间戳，按照指定的格式，转换为时间对象。</p>
<p>举例1：（参数是字符串）</p>
<pre class="highlight"><span class="line"><span class="keyword">const</span> date11 = <span class="keyword">new</span> <span class="title class_">Date</span>(<span class="string">&#x27;2020/02/17 21:00:00&#x27;</span>);</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(date11); <span class="comment">// Mon Feb 17 2020 21:00:00 GMT+0800 (中国标准时间)</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> date12 = <span class="keyword">new</span> <span class="title class_">Date</span>(<span class="string">&#x27;2020/04/19&#x27;</span>); <span class="comment">// 返回的就是四月</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(date12); <span class="comment">// Sun Apr 19 2020 00:00:00 GMT+0800 (中国标准时间)</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> date13 = <span class="keyword">new</span> <span class="title class_">Date</span>(<span class="string">&#x27;2020-05-20&#x27;</span>);</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(date13); <span class="comment">// Wed May 20 2020 08:00:00 GMT+0800 (中国标准时间)</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> date14 = <span class="keyword">new</span> <span class="title class_">Date</span>(<span class="string">&#x27;Wed Jan 27 2017 12:00:00 GMT+0800 (中国标准时间)&#x27;</span>);</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(date14); <span class="comment">// Fri Jan 27 2017 12:00:00 GMT+0800 (中国标准时间)</span></span><br></pre>


<p>举例2：（参数是多个数字）</p>
<pre class="highlight"><span class="line"><span class="keyword">const</span> date21 = <span class="keyword">new</span> <span class="title class_">Date</span>(<span class="number">2020</span>, <span class="number">2</span>, <span class="number">18</span>); <span class="comment">// 注意，第二个参数返回的是三月，不是二月</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(date21); <span class="comment">// Wed Mar 18 2020 00:00:00 GMT+0800 (中国标准时间)</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> date22 = <span class="keyword">new</span> <span class="title class_">Date</span>(<span class="number">2020</span>, <span class="number">3</span>, <span class="number">18</span>, <span class="number">22</span>, <span class="number">59</span>, <span class="number">58</span>);</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(date22); <span class="comment">// Sat Apr 18 2020 22:59:58 GMT+0800 (中国标准时间)</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> params = [<span class="number">2020</span>, <span class="number">06</span>, <span class="number">12</span>, <span class="number">16</span>, <span class="number">20</span>, <span class="number">59</span>];</span><br><span class="line"><span class="keyword">const</span> date23 = <span class="keyword">new</span> <span class="title class_">Date</span>(...params);</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(date23); <span class="comment">// Sun Jul 12 2020 16:20:59 GMT+0800 (中国标准时间)</span></span><br></pre>


<p>举例3：（参数是时间戳）</p>
<pre class="highlight"><span class="line"><span class="keyword">const</span> date31 = <span class="keyword">new</span> <span class="title class_">Date</span>(<span class="number">1591950413388</span>);</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(date31); <span class="comment">// Fri Jun 12 2020 16:26:53 GMT+0800 (中国标准时间)</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 先把时间对象转换成时间戳，然后把时间戳转换成时间对象</span></span><br><span class="line"><span class="keyword">const</span> timestamp = <span class="keyword">new</span> <span class="title class_">Date</span>().<span class="title function_">getTime</span>();</span><br><span class="line"><span class="keyword">const</span> date32 = <span class="keyword">new</span> <span class="title class_">Date</span>(timestamp);</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(date32); <span class="comment">// Fri Jun 12 2020 16:28:21 GMT+0800 (中国标准时间)</span></span><br></pre>





<h2 id="日期的格式化"><a href="#日期的格式化" class="headerlink" title="日期的格式化"></a>日期的格式化</h2><p>上一段内容里，我们获取到了 Date <strong>对象</strong>，但这个对象，打印出来的结果并不是特别直观。</p>
<p>如果我们需要获取日期的<strong>指定部分</strong>，就需要用到 Date对象自带的方法。</p>
<p>获取了日期指定的部分之后，我们就可以让日期按照指定的格式，进行展示（即日期的格式化）。比如说，我期望能以 <code>2020-02-02 19:30:59</code> 这种格式进行展示。</p>
<p>在这之前，我们先来看看 Date 对象有哪些方法。</p>
<h3 id="Date对象的方法"><a href="#Date对象的方法" class="headerlink" title="Date对象的方法"></a>Date对象的方法</h3><p>Date对象 有如下方法，可以获取日期和时间的<strong>指定部分</strong>：</p>
<table>
<thead>
<tr>
<th>方法名</th>
<th>含义</th>
<th>备注</th>
</tr>
</thead>
<tbody><tr>
<td>getFullYear()</td>
<td>获取年份</td>
<td></td>
</tr>
<tr>
<td>getMonth()</td>
<td><strong>获取月： 0-11</strong></td>
<td>0代表一月</td>
</tr>
<tr>
<td>getDate()</td>
<td><strong>获取日：1-31</strong></td>
<td>获取的是几号</td>
</tr>
<tr>
<td>getDay()</td>
<td><strong>获取星期：0-6</strong></td>
<td>0代表周日，1代表周一</td>
</tr>
<tr>
<td>getHours()</td>
<td>获取小时：0-23</td>
<td></td>
</tr>
<tr>
<td>getMinutes()</td>
<td>获取分钟：0-59</td>
<td></td>
</tr>
<tr>
<td>getSeconds()</td>
<td>获取秒：0-59</td>
<td></td>
</tr>
<tr>
<td>getMilliseconds()</td>
<td>获取毫秒</td>
<td>1s &#x3D; 1000ms</td>
</tr>
</tbody></table>
<p><strong>代码举例</strong>：</p>
<pre class="highlight"><span class="line"><span class="comment">// 我在执行这行代码时，当前时间为 2019年2月4日，周一，13:23:52</span></span><br><span class="line"><span class="keyword">var</span> myDate = <span class="keyword">new</span> <span class="title class_">Date</span>();</span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(myDate); <span class="comment">// 打印结果：Mon Feb 04 2019 13:23:52 GMT+0800 (中国标准时间)</span></span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(myDate.<span class="title function_">getFullYear</span>()); <span class="comment">// 打印结果：2019</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(myDate.<span class="title function_">getMonth</span>() + <span class="number">1</span>); <span class="comment">// 打印结果：2</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(myDate.<span class="title function_">getDate</span>()); <span class="comment">// 打印结果：4</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> dayArr  = [<span class="string">&#x27;星期日&#x27;</span>, <span class="string">&#x27;星期一&#x27;</span>, <span class="string">&#x27;星期二&#x27;</span>, <span class="string">&#x27;星期三&#x27;</span>, <span class="string">&#x27;星期四&#x27;</span>,<span class="string">&#x27;星期五&#x27;</span>, <span class="string">&#x27;星期六&#x27;</span>];</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(myDate.<span class="title function_">getDay</span>()); <span class="comment">// 打印结果：1</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(dayArr[myDate.<span class="title function_">getDay</span>()]); <span class="comment">// 打印结果：星期一</span></span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(myDate.<span class="title function_">getHours</span>()); <span class="comment">// 打印结果：13</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(myDate.<span class="title function_">getMinutes</span>()); <span class="comment">// 打印结果：23</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(myDate.<span class="title function_">getSeconds</span>()); <span class="comment">// 打印结果：52</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(myDate.<span class="title function_">getMilliseconds</span>()); <span class="comment">// 打印结果：393</span></span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(myDate.<span class="title function_">getTime</span>()); <span class="comment">// 获取时间戳。打印结果：1549257832393</span></span><br></pre>

<p>获取了日期和时间的指定部分之后，我们把它们用字符串拼接起来，就可以按照自己想要的格式，来展示日期。</p>
<h3 id="举例：年月日的格式化"><a href="#举例：年月日的格式化" class="headerlink" title="举例：年月日的格式化"></a>举例：年月日的格式化</h3><p>代码举例：</p>
<pre class="highlight"><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="title function_">formatDate</span>());</span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">    方法：日期格式化。</span></span><br><span class="line"><span class="comment">    格式要求：今年是：2020年02月02日 08:57:09 星期日</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">formatDate</span>(<span class="params"></span>) &#123;</span><br><span class="line">    <span class="keyword">var</span> date = <span class="keyword">new</span> <span class="title class_">Date</span>();</span><br><span class="line"></span><br><span class="line">    <span class="keyword">var</span> year = date.<span class="title function_">getFullYear</span>(); <span class="comment">// 年</span></span><br><span class="line">    <span class="keyword">var</span> month = date.<span class="title function_">getMonth</span>() + <span class="number">1</span>; <span class="comment">// 月</span></span><br><span class="line">    <span class="keyword">var</span> day = date.<span class="title function_">getDate</span>(); <span class="comment">// 日</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">var</span> week = date.<span class="title function_">getDay</span>(); <span class="comment">// 星期几</span></span><br><span class="line">    <span class="keyword">var</span> weekArr = [<span class="string">&#x27;星期日&#x27;</span>, <span class="string">&#x27;星期一&#x27;</span>, <span class="string">&#x27;星期二&#x27;</span>, <span class="string">&#x27;星期三&#x27;</span>, <span class="string">&#x27;星期四&#x27;</span>, <span class="string">&#x27;星期五&#x27;</span>, <span class="string">&#x27;星期六&#x27;</span>];</span><br><span class="line"></span><br><span class="line">    <span class="keyword">var</span> hour = date.<span class="title function_">getHours</span>(); <span class="comment">// 时</span></span><br><span class="line">    hour = hour &lt; <span class="number">10</span> ? <span class="string">&#x27;0&#x27;</span> + hour : hour; <span class="comment">// 如果只有一位，则前面补零</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">var</span> minute = date.<span class="title function_">getMinutes</span>(); <span class="comment">// 分</span></span><br><span class="line">    minute = minute &lt; <span class="number">10</span> ? <span class="string">&#x27;0&#x27;</span> + minute : minute; <span class="comment">// 如果只有一位，则前面补零</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">var</span> second = date.<span class="title function_">getSeconds</span>(); <span class="comment">// 秒</span></span><br><span class="line">    second = second &lt; <span class="number">10</span> ? <span class="string">&#x27;0&#x27;</span> + second : second; <span class="comment">// 如果只有一位，则前面补零</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">var</span> result = <span class="string">&#x27;今天是：&#x27;</span> + year + <span class="string">&#x27;年&#x27;</span> + month + <span class="string">&#x27;月&#x27;</span> + day + <span class="string">&#x27;日 &#x27;</span> + hour + <span class="string">&#x27;:&#x27;</span> + minute + <span class="string">&#x27;:&#x27;</span> + second + <span class="string">&#x27; &#x27;</span> + weekArr[week];</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> result;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre>




<h2 id="获取时间戳"><a href="#获取时间戳" class="headerlink" title="获取时间戳"></a>获取时间戳</h2><h3 id="时间戳的定义和作用"><a href="#时间戳的定义和作用" class="headerlink" title="时间戳的定义和作用"></a>时间戳的定义和作用</h3><p><strong>时间戳</strong>：指的是从格林威治标准时间的<code>1970年1月1日，0时0分0秒</code>到当前日期所花费的<strong>毫秒数</strong>（1秒 &#x3D; 1000毫秒）。</p>
<p>计算机底层在保存时间时，使用的都是时间戳。时间戳的存在，就是为了<strong>统一</strong>时间的单位。</p>
<p>我们经常会利用时间戳来计算时间，因为它更精确。而且，在实战开发中，接口返回给前端的日期数据，都是以时间戳的形式。</p>
<p>我们再来看下面这样的代码：</p>
<pre class="highlight"><span class="line"><span class="keyword">var</span> myDate = <span class="keyword">new</span> <span class="title class_">Date</span>(<span class="string">&quot;1970/01/01 0:0:0&quot;</span>);</span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(myDate.<span class="title function_">getTime</span>()); <span class="comment">// 获取时间戳</span></span><br></pre>

<p>打印结果（可能会让你感到惊讶）</p>
<pre class="highlight"><span class="line">-<span class="number">28800000</span></span><br></pre>

<p>为啥打印结果是<code>-28800000</code>，而不是<code>0</code>呢？这是因为，我们的当前代码，是在中文环境下运行的，与英文时间会存在<strong>8个小时的时差</strong>（中文时间比英文时间早了八个小时）。如果代码是在英文环境下运行，打印结果就是<code>0</code>。</p>
<h3 id="getTime-：获取时间戳"><a href="#getTime-：获取时间戳" class="headerlink" title="getTime()：获取时间戳"></a>getTime()：获取时间戳</h3><p><code>getTime()</code>  获取日期对象的<strong>时间戳</strong>（单位：毫秒）。这个方法在实战开发中，用得比较多。但还有比它更常用的写法，我们往下看。</p>
<h3 id="获取-Date-对象的时间戳"><a href="#获取-Date-对象的时间戳" class="headerlink" title="获取 Date 对象的时间戳"></a>获取 Date 对象的时间戳</h3><p>代码演示：</p>
<pre class="highlight"><span class="line"><span class="comment">// 方式一：获取 Date 对象的时间戳（最常用的写法）</span></span><br><span class="line"><span class="keyword">const</span> timestamp1 = +<span class="keyword">new</span> <span class="title class_">Date</span>();</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(timestamp1); <span class="comment">// 打印结果举例：1589448165370</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 方式二：获取 Date 对象的时间戳（较常用的写法）</span></span><br><span class="line"><span class="keyword">const</span> timestamp2 = <span class="keyword">new</span> <span class="title class_">Date</span>().<span class="title function_">getTime</span>();</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(timestamp2); <span class="comment">// 打印结果举例：1589448165370</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 方式三：获取 Date 对象的时间戳</span></span><br><span class="line"><span class="keyword">const</span> timestamp3 = <span class="keyword">new</span> <span class="title class_">Date</span>().<span class="title function_">valueOf</span>();</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(timestamp3); <span class="comment">// 打印结果举例：1589448165370</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 方式4：获取 Date 对象的时间戳</span></span><br><span class="line"><span class="keyword">const</span> timestamp4 = <span class="keyword">new</span> <span class="title class_">Date</span>() * <span class="number">1</span>;</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(timestamp4); <span class="comment">// 打印结果举例：1589448165370</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 方式5：获取 Date 对象的时间戳</span></span><br><span class="line"><span class="keyword">const</span> timestamp5 = <span class="title class_">Number</span>(<span class="keyword">new</span> <span class="title class_">Date</span>());</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(timestamp5); <span class="comment">// 打印结果举例：1589448165370</span></span><br></pre>

<p>上面这五种写法都可以获取任意 Date 对象的时间戳，最常见的写法是<strong>方式一</strong>，其次是方式二。</p>
<p>根据前面所讲的关于「时间戳」的概念，上方代码获取到的时间戳指的是：从 <code>1970年1月1日，0时0分0秒</code> 到现在所花费的总毫秒数。</p>
<h3 id="获取当前时间的时间戳"><a href="#获取当前时间的时间戳" class="headerlink" title="获取当前时间的时间戳"></a>获取当前时间的时间戳</h3><p>如果我们要获取<strong>当前时间</strong>的时间戳，除了上面的几种方式之外，还有另一种方式。代码如下：</p>
<pre class="highlight"><span class="line"><span class="comment">// 方式六：获取当前时间的时间戳（很常用的写法）</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="title class_">Date</span>.<span class="title function_">now</span>()); <span class="comment">// 打印结果举例：1589448165370</span></span><br></pre>

<p>上面这种方式六，用得也很多。只不过，<code>Date.now()</code>是H5标准中新增的特性，如果你的项目需要兼容低版本的IE浏览器，就不要用了。这年头，谁还用IE呢？</p>
<h3 id="利用时间戳检测代码的执行时间"><a href="#利用时间戳检测代码的执行时间" class="headerlink" title="利用时间戳检测代码的执行时间"></a>利用时间戳检测代码的执行时间</h3><p>我们可以在业务代码的前面定义 <code>时间戳1</code>，在业务代码的后面定义 <code>时间戳2</code>。把这两个时间戳相减，就能得出业务代码的执行时间。</p>
<h3 id="format"><a href="#format" class="headerlink" title="format()"></a>format()</h3><p>将时间对象转换为指定格式。</p>
<p>参考链接：<a target="_blank" rel="noopener" href="https://www.cnblogs.com/tugenhua0707/p/3776808.html">https://www.cnblogs.com/tugenhua0707/p/3776808.html</a></p>
<h2 id="练习"><a href="#练习" class="headerlink" title="练习"></a>练习</h2><h3 id="举例1：模拟日历"><a href="#举例1：模拟日历" class="headerlink" title="举例1：模拟日历"></a>举例1：模拟日历</h3><p>要求每天打开这个页面，都能定时显示当前的日期。</p>
<p>代码实现：</p>
<pre class="highlight"><span class="line"><span class="meta">&lt;!DOCTYPE <span class="keyword">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">head</span> <span class="attr">lang</span>=<span class="string">&quot;en&quot;</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">&quot;UTF-8&quot;</span> /&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">title</span>&gt;</span><span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">style</span>&gt;</span><span class="language-css"></span></span><br><span class="line"><span class="language-css">            <span class="selector-tag">div</span> &#123;</span></span><br><span class="line"><span class="language-css">                <span class="attribute">width</span>: <span class="number">800px</span>;</span></span><br><span class="line"><span class="language-css">                <span class="attribute">margin</span>: <span class="number">200px</span> auto;</span></span><br><span class="line"><span class="language-css">                <span class="attribute">color</span>: red;</span></span><br><span class="line"><span class="language-css">                <span class="attribute">text-align</span>: center;</span></span><br><span class="line"><span class="language-css">                <span class="attribute">font</span>: <span class="number">600</span> <span class="number">30px</span>/<span class="number">30px</span> <span class="string">&#x27;simsun&#x27;</span>;</span></span><br><span class="line"><span class="language-css">            &#125;</span></span><br><span class="line"><span class="language-css">        </span><span class="tag">&lt;/<span class="name">style</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">div</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"></span><br><span class="line">        <span class="tag">&lt;<span class="name">script</span>&gt;</span><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">            <span class="comment">//模拟日历</span></span></span><br><span class="line"><span class="language-javascript">            <span class="comment">//需求：每天打开这个页面都能定时显示年月日和星期几</span></span></span><br><span class="line"><span class="language-javascript">            <span class="keyword">function</span> <span class="title function_">getCurrentDate</span>(<span class="params"></span>) &#123;</span></span><br><span class="line"><span class="language-javascript">                <span class="comment">//1.创建一个当前日期的日期对象</span></span></span><br><span class="line"><span class="language-javascript">                <span class="keyword">const</span> date = <span class="keyword">new</span> <span class="title class_">Date</span>();</span></span><br><span class="line"><span class="language-javascript">                <span class="comment">//2.然后获取其中的年、月、日和星期</span></span></span><br><span class="line"><span class="language-javascript">                <span class="keyword">const</span> year = date.<span class="title function_">getFullYear</span>();</span></span><br><span class="line"><span class="language-javascript">                <span class="keyword">const</span> month = date.<span class="title function_">getMonth</span>();</span></span><br><span class="line"><span class="language-javascript">                <span class="keyword">const</span> hao = date.<span class="title function_">getDate</span>();</span></span><br><span class="line"><span class="language-javascript">                <span class="keyword">const</span> week = date.<span class="title function_">getDay</span>();</span></span><br><span class="line"><span class="language-javascript">                <span class="comment">//        console.log(year+&quot; &quot;+month+&quot; &quot;+hao+&quot; &quot;+week);</span></span></span><br><span class="line"><span class="language-javascript">                <span class="comment">//3.赋值给div</span></span></span><br><span class="line"><span class="language-javascript">                <span class="keyword">const</span> arr = [<span class="string">&#x27;星期日&#x27;</span>, <span class="string">&#x27;星期一&#x27;</span>, <span class="string">&#x27;星期二&#x27;</span>, <span class="string">&#x27;星期三&#x27;</span>, <span class="string">&#x27;星期四&#x27;</span>, <span class="string">&#x27;星期五&#x27;</span>, <span class="string">&#x27;星期六&#x27;</span>];</span></span><br><span class="line"><span class="language-javascript">                <span class="keyword">const</span> div = <span class="variable language_">document</span>.<span class="title function_">getElementsByTagName</span>(<span class="string">&#x27;div&#x27;</span>)[<span class="number">0</span>];</span></span><br><span class="line"><span class="language-javascript">                <span class="keyword">return</span> <span class="string">&#x27;今天是：&#x27;</span> + year + <span class="string">&#x27;年&#x27;</span> + (month + <span class="number">1</span>) + <span class="string">&#x27;月&#x27;</span> + hao + <span class="string">&#x27;日 &#x27;</span> + arr[week];</span></span><br><span class="line"><span class="language-javascript">            &#125;</span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">            <span class="keyword">const</span> div = <span class="variable language_">document</span>.<span class="title function_">getElementsByTagName</span>(<span class="string">&#x27;div&#x27;</span>)[<span class="number">0</span>];</span></span><br><span class="line"><span class="language-javascript">            div.<span class="property">innerText</span> = <span class="title function_">getCurrentDate</span>();</span></span><br><span class="line"><span class="language-javascript">        </span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre>

<p>实现效果：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20180202_1110.png" class="lozad post-image"src="http://img.smyhvae.com/20180202_1110.png"></p>
<h3 id="举例2：发布会倒计时"><a href="#举例2：发布会倒计时" class="headerlink" title="举例2：发布会倒计时"></a>举例2：发布会倒计时</h3><p>实现思路：</p>
<ul>
<li><p>设置一个定时器，每间隔1毫秒就自动刷新一次div的内容。</p>
</li>
<li><p>核心算法：输入的时间戳减去当前的时间戳，就是剩余时间（即倒计时），然后转换成时分秒。</p>
</li>
</ul>
<p>代码实现：</p>
<pre class="highlight"><span class="line"><span class="meta">&lt;!DOCTYPE <span class="keyword">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">head</span> <span class="attr">lang</span>=<span class="string">&quot;en&quot;</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">&quot;UTF-8&quot;</span> /&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">title</span>&gt;</span><span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">style</span>&gt;</span><span class="language-css"></span></span><br><span class="line"><span class="language-css">            <span class="selector-tag">div</span> &#123;</span></span><br><span class="line"><span class="language-css">                <span class="attribute">width</span>: <span class="number">1210px</span>;</span></span><br><span class="line"><span class="language-css">                <span class="attribute">margin</span>: <span class="number">200px</span> auto;</span></span><br><span class="line"><span class="language-css">                <span class="attribute">color</span>: red;</span></span><br><span class="line"><span class="language-css">                <span class="attribute">text-align</span>: center;</span></span><br><span class="line"><span class="language-css">                <span class="attribute">font</span>: <span class="number">600</span> <span class="number">30px</span>/<span class="number">30px</span> <span class="string">&#x27;simsun&#x27;</span>;</span></span><br><span class="line"><span class="language-css">            &#125;</span></span><br><span class="line"><span class="language-css">        </span><span class="tag">&lt;/<span class="name">style</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">div</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"></span><br><span class="line">        <span class="tag">&lt;<span class="name">script</span>&gt;</span><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">            <span class="keyword">var</span> div = <span class="variable language_">document</span>.<span class="title function_">getElementsByTagName</span>(<span class="string">&#x27;div&#x27;</span>)[<span class="number">0</span>];</span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">            <span class="keyword">var</span> timer = <span class="built_in">setInterval</span>(<span class="function">() =&gt;</span> &#123;</span></span><br><span class="line"><span class="language-javascript">                <span class="title function_">countDown</span>(<span class="string">&#x27;2022/02/03 11:20:00&#x27;</span>);</span></span><br><span class="line"><span class="language-javascript">            &#125;, <span class="number">1</span>);</span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">            <span class="keyword">function</span> <span class="title function_">countDown</span>(<span class="params">myTime</span>) &#123;</span></span><br><span class="line"><span class="language-javascript">                <span class="keyword">var</span> nowTime = <span class="keyword">new</span> <span class="title class_">Date</span>();</span></span><br><span class="line"><span class="language-javascript">                <span class="keyword">var</span> future = <span class="keyword">new</span> <span class="title class_">Date</span>(myTime);</span></span><br><span class="line"><span class="language-javascript">                <span class="keyword">var</span> timeSum = future.<span class="title function_">getTime</span>() - nowTime.<span class="title function_">getTime</span>(); <span class="comment">//获取时间差：发布会时间减去此刻的毫秒值</span></span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">                <span class="keyword">var</span> day = <span class="built_in">parseInt</span>(timeSum / <span class="number">1000</span> / <span class="number">60</span> / <span class="number">60</span> / <span class="number">24</span>); <span class="comment">// 天</span></span></span><br><span class="line"><span class="language-javascript">                <span class="keyword">var</span> hour = <span class="built_in">parseInt</span>((timeSum / <span class="number">1000</span> / <span class="number">60</span> / <span class="number">60</span>) % <span class="number">24</span>); <span class="comment">// 时</span></span></span><br><span class="line"><span class="language-javascript">                <span class="keyword">var</span> minu = <span class="built_in">parseInt</span>((timeSum / <span class="number">1000</span> / <span class="number">60</span>) % <span class="number">60</span>); <span class="comment">// 分</span></span></span><br><span class="line"><span class="language-javascript">                <span class="keyword">var</span> sec = <span class="built_in">parseInt</span>((timeSum / <span class="number">1000</span>) % <span class="number">60</span>); <span class="comment">// 秒</span></span></span><br><span class="line"><span class="language-javascript">                <span class="keyword">var</span> millsec = <span class="built_in">parseInt</span>(timeSum % <span class="number">1000</span>); <span class="comment">// 毫秒</span></span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">                <span class="comment">//细节处理：所有的时间小于10的时候，在前面自动补0，毫秒值要补双0（比如如，把 8 秒改成 08 秒）</span></span></span><br><span class="line"><span class="language-javascript">                day = day &lt; <span class="number">10</span> ? <span class="string">&#x27;0&#x27;</span> + day : day; <span class="comment">//day小于10吗？如果小于，就补0；如果不小于，就是day本身</span></span></span><br><span class="line"><span class="language-javascript">                hour = hour &lt; <span class="number">10</span> ? <span class="string">&#x27;0&#x27;</span> + hour : hour;</span></span><br><span class="line"><span class="language-javascript">                minu = minu &lt; <span class="number">10</span> ? <span class="string">&#x27;0&#x27;</span> + minu : minu;</span></span><br><span class="line"><span class="language-javascript">                sec = sec &lt; <span class="number">10</span> ? <span class="string">&#x27;0&#x27;</span> + sec : sec;</span></span><br><span class="line"><span class="language-javascript">                <span class="keyword">if</span> (millsec &lt; <span class="number">10</span>) &#123;</span></span><br><span class="line"><span class="language-javascript">                    millsec = <span class="string">&#x27;00&#x27;</span> + millsec;</span></span><br><span class="line"><span class="language-javascript">                &#125; <span class="keyword">else</span> <span class="keyword">if</span> (millsec &lt; <span class="number">100</span>) &#123;</span></span><br><span class="line"><span class="language-javascript">                    millsec = <span class="string">&#x27;0&#x27;</span> + millsec;</span></span><br><span class="line"><span class="language-javascript">                &#125;</span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">                <span class="comment">// 兜底处理</span></span></span><br><span class="line"><span class="language-javascript">                <span class="keyword">if</span> (timeSum &lt; <span class="number">0</span>) &#123;</span></span><br><span class="line"><span class="language-javascript">                    div.<span class="property">innerHTML</span> = <span class="string">&#x27;距离苹果发布会还有00天00小时00分00秒000毫秒&#x27;</span>;</span></span><br><span class="line"><span class="language-javascript">                    <span class="built_in">clearInterval</span>(timer);</span></span><br><span class="line"><span class="language-javascript">                    <span class="keyword">return</span>;</span></span><br><span class="line"><span class="language-javascript">                &#125;</span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">                <span class="comment">// 前端要显示的文案</span></span></span><br><span class="line"><span class="language-javascript">                div.<span class="property">innerHTML</span> = <span class="string">&#x27;距离苹果发布会还有&#x27;</span> + day + <span class="string">&#x27;天&#x27;</span> + hour + <span class="string">&#x27;小时&#x27;</span> + minu + <span class="string">&#x27;分&#x27;</span> + sec + <span class="string">&#x27;秒&#x27;</span> + millsec + <span class="string">&#x27;毫秒&#x27;</span>;</span></span><br><span class="line"><span class="language-javascript">            &#125;</span></span><br><span class="line"><span class="language-javascript">        </span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br><span class="line"></span><br></pre>

<p>实现效果：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20180202_1130.gif" class="lozad post-image"src="http://img.smyhvae.com/20180202_1130.gif"></p>
<h2 id="Moment-js"><a href="#Moment-js" class="headerlink" title="Moment.js"></a>Moment.js</h2><p>Moment.js 是一个轻量级的JavaScript时间库，我们可以利用它很方便地进行时间操作，提升开发效率。</p>
<ul>
<li>中文官网：<a target="_blank" rel="noopener" href="http://momentjs.cn/">http://momentjs.cn/</a></li>
</ul>
<p>使用举例：</p>
<pre class="highlight"><span class="line"><span class="meta">&lt;!DOCTYPE <span class="keyword">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span> <span class="attr">lang</span>=<span class="string">&quot;en&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">head</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">&quot;UTF-8&quot;</span> /&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">meta</span> <span class="attr">name</span>=<span class="string">&quot;viewport&quot;</span> <span class="attr">content</span>=<span class="string">&quot;width=device-width, initial-scale=1.0&quot;</span> /&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">title</span>&gt;</span>Document<span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">script</span> <span class="attr">src</span>=<span class="string">&quot;https://cdn.bootcdn.net/ajax/libs/moment.js/2.26.0/moment.min.js&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">script</span>&gt;</span><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">            <span class="comment">// 按照指定的格式，格式化当前时间</span></span></span><br><span class="line"><span class="language-javascript">            <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="title function_">moment</span>().<span class="title function_">format</span>(<span class="string">&#x27;YYYY-MM-DD HH:mm:ss&#x27;</span>)); <span class="comment">// 打印结果举例：2020-06-12 16:38:38</span></span></span><br><span class="line"><span class="language-javascript">            <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="keyword">typeof</span> <span class="title function_">moment</span>().<span class="title function_">format</span>(<span class="string">&#x27;YYYY-MM-DD HH:mm:ss&#x27;</span>)); <span class="comment">// 打印结果：string</span></span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">            <span class="comment">// 按照指定的格式，格式化指定的时间</span></span></span><br><span class="line"><span class="language-javascript">            <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="title function_">moment</span>(<span class="string">&#x27;2020/06/12 18:01:59&#x27;</span>).<span class="title function_">format</span>(<span class="string">&#x27;YYYY-MM-DD HH:mm:ss&#x27;</span>)); <span class="comment">// 打印结果：2020-06-12 18:01:59</span></span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">            <span class="comment">// 按照指定的格式，获取七天后的时间</span></span></span><br><span class="line"><span class="language-javascript">            <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="title function_">moment</span>().<span class="title function_">add</span>(<span class="number">7</span>, <span class="string">&#x27;days&#x27;</span>).<span class="title function_">format</span>(<span class="string">&#x27;YYYY-MM-DD hh:mm:ss&#x27;</span>)); <span class="comment">// 打印结果举例：2020-06-19 04:43:56</span></span></span><br><span class="line"><span class="language-javascript">        </span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br><span class="line"></span><br></pre>


<h2 id="赞赏作者"><a href="#赞赏作者" class="headerlink" title="赞赏作者"></a>赞赏作者</h2><p>创作不易，你的赞赏和认可，是我更新的最大动力：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="https://img.smyhvae.com/20220401_1800.jpg" class="lozad post-image"src="https://img.smyhvae.com/20220401_1800.jpg"></p>

  </div>
  <div>
    
      <div 
        class="post-note note-warning copyright" 
        style="margin-top: 42px">
        <p>
          <span style="font-weight: bold;">作者：</span><a 
            target="_blank" 
            rel="nofollow noopener noreferrer" 
            href="/about">
            zwl5670
          </a>
        </p>
        <p>
          <span style="font-weight: bold;">文章链接：</span><a 
            target="_blank" 
            rel="nofollow noopener noreferrer" 
            href="https://zwl5670.gitee.io/2004/03/01/04-JavaScript%E5%9F%BA%E7%A1%80/17-%E5%86%85%E7%BD%AE%E5%AF%B9%E8%B1%A1%EF%BC%9ADate/">
            https://zwl5670.gitee.io/2004/03/01/04-JavaScript%E5%9F%BA%E7%A1%80/17-%E5%86%85%E7%BD%AE%E5%AF%B9%E8%B1%A1%EF%BC%9ADate/
          </a>
        </p>
        <p><span style="font-weight: bold;">版权声明：</span>本博客所有文章除特别声明外，均采用<a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh">CC BY-NC-SA 4.0 协议</a>。转载请注明出处！</p>
      </div>
    
  </div>
</article>
<div class="nav">
  
    <div class="nav-item-prev">
      <a 
        href="/2004/03/01/04-JavaScript%E5%9F%BA%E7%A1%80/15-%E5%86%85%E7%BD%AE%E5%AF%B9%E8%B1%A1%20String%EF%BC%9A%E5%AD%97%E7%AC%A6%E4%B8%B2%E7%9A%84%E5%B8%B8%E8%A7%81%E6%96%B9%E6%B3%95/" 
        class="nav-link">
        <i class="iconfont icon-left nav-prev-icon"></i>
        <div>
          <div class="nav-label">上一篇</div>
          
            <div class="nav-title">15-内置对象 String：字符串的常见方法 </div>
          
        </div>
      </a>
    </div>
  
  
    <div class="nav-item-next">
      <a 
        href="/2004/03/01/04-JavaScript%E5%9F%BA%E7%A1%80/18-%E6%95%B0%E7%BB%84%E7%AE%80%E4%BB%8B/" 
        class="nav-link">
        <div>
          <div class="nav-label">下一篇</div>
          
            <div class="nav-title">18-数组简介 </div>
          
        </div>
        <i class="iconfont icon-right nav-next-icon"></i>
      </a>
    </div>
  
</div>

<div 
  class="card card-content toc-card" 
  id="mobiletoc">
  <div class="toc-header">
  <i 
    class="iconfont icon-menu" 
    style="padding-right: 2px;">
  </i>目录
</div>
<ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%86%85%E7%BD%AE%E5%AF%B9%E8%B1%A1%EF%BC%9ADate"><span class="toc-text">内置对象：Date</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%88%9B%E5%BB%BADate%E5%AF%B9%E8%B1%A1"><span class="toc-text">创建Date对象</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%86%99%E6%B3%95%E4%B8%80%EF%BC%9A%E4%B8%8D%E4%BC%A0%E9%80%92%E5%8F%82%E6%95%B0%E6%97%B6%EF%BC%8C%E5%88%99%E8%8E%B7%E5%8F%96%E7%B3%BB%E7%BB%9F%E7%9A%84%E5%BD%93%E5%89%8D%E6%97%B6%E9%97%B4%E5%AF%B9%E8%B1%A1"><span class="toc-text">写法一：不传递参数时，则获取系统的当前时间对象</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%86%99%E6%B3%95%E4%BA%8C%EF%BC%9A%E4%BC%A0%E9%80%92%E5%8F%82%E6%95%B0"><span class="toc-text">写法二：传递参数</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%97%A5%E6%9C%9F%E7%9A%84%E6%A0%BC%E5%BC%8F%E5%8C%96"><span class="toc-text">日期的格式化</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#Date%E5%AF%B9%E8%B1%A1%E7%9A%84%E6%96%B9%E6%B3%95"><span class="toc-text">Date对象的方法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%B8%BE%E4%BE%8B%EF%BC%9A%E5%B9%B4%E6%9C%88%E6%97%A5%E7%9A%84%E6%A0%BC%E5%BC%8F%E5%8C%96"><span class="toc-text">举例：年月日的格式化</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%8E%B7%E5%8F%96%E6%97%B6%E9%97%B4%E6%88%B3"><span class="toc-text">获取时间戳</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%97%B6%E9%97%B4%E6%88%B3%E7%9A%84%E5%AE%9A%E4%B9%89%E5%92%8C%E4%BD%9C%E7%94%A8"><span class="toc-text">时间戳的定义和作用</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#getTime-%EF%BC%9A%E8%8E%B7%E5%8F%96%E6%97%B6%E9%97%B4%E6%88%B3"><span class="toc-text">getTime()：获取时间戳</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%8E%B7%E5%8F%96-Date-%E5%AF%B9%E8%B1%A1%E7%9A%84%E6%97%B6%E9%97%B4%E6%88%B3"><span class="toc-text">获取 Date 对象的时间戳</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%8E%B7%E5%8F%96%E5%BD%93%E5%89%8D%E6%97%B6%E9%97%B4%E7%9A%84%E6%97%B6%E9%97%B4%E6%88%B3"><span class="toc-text">获取当前时间的时间戳</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%88%A9%E7%94%A8%E6%97%B6%E9%97%B4%E6%88%B3%E6%A3%80%E6%B5%8B%E4%BB%A3%E7%A0%81%E7%9A%84%E6%89%A7%E8%A1%8C%E6%97%B6%E9%97%B4"><span class="toc-text">利用时间戳检测代码的执行时间</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#format"><span class="toc-text">format()</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%BB%83%E4%B9%A0"><span class="toc-text">练习</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%B8%BE%E4%BE%8B1%EF%BC%9A%E6%A8%A1%E6%8B%9F%E6%97%A5%E5%8E%86"><span class="toc-text">举例1：模拟日历</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%B8%BE%E4%BE%8B2%EF%BC%9A%E5%8F%91%E5%B8%83%E4%BC%9A%E5%80%92%E8%AE%A1%E6%97%B6"><span class="toc-text">举例2：发布会倒计时</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Moment-js"><span class="toc-text">Moment.js</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%B5%9E%E8%B5%8F%E4%BD%9C%E8%80%85"><span class="toc-text">赞赏作者</span></a></li></ol>
</div>
            </main>
            <aside class="right-column">
              <div class="sticky-widescreen">
  
  
    <article class="card card-content toc-card">
      <div class="toc-header">
  <i 
    class="iconfont icon-menu" 
    style="padding-right: 2px;">
  </i>目录
</div>
<ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%86%85%E7%BD%AE%E5%AF%B9%E8%B1%A1%EF%BC%9ADate"><span class="toc-text">内置对象：Date</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%88%9B%E5%BB%BADate%E5%AF%B9%E8%B1%A1"><span class="toc-text">创建Date对象</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%86%99%E6%B3%95%E4%B8%80%EF%BC%9A%E4%B8%8D%E4%BC%A0%E9%80%92%E5%8F%82%E6%95%B0%E6%97%B6%EF%BC%8C%E5%88%99%E8%8E%B7%E5%8F%96%E7%B3%BB%E7%BB%9F%E7%9A%84%E5%BD%93%E5%89%8D%E6%97%B6%E9%97%B4%E5%AF%B9%E8%B1%A1"><span class="toc-text">写法一：不传递参数时，则获取系统的当前时间对象</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%86%99%E6%B3%95%E4%BA%8C%EF%BC%9A%E4%BC%A0%E9%80%92%E5%8F%82%E6%95%B0"><span class="toc-text">写法二：传递参数</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%97%A5%E6%9C%9F%E7%9A%84%E6%A0%BC%E5%BC%8F%E5%8C%96"><span class="toc-text">日期的格式化</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#Date%E5%AF%B9%E8%B1%A1%E7%9A%84%E6%96%B9%E6%B3%95"><span class="toc-text">Date对象的方法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%B8%BE%E4%BE%8B%EF%BC%9A%E5%B9%B4%E6%9C%88%E6%97%A5%E7%9A%84%E6%A0%BC%E5%BC%8F%E5%8C%96"><span class="toc-text">举例：年月日的格式化</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%8E%B7%E5%8F%96%E6%97%B6%E9%97%B4%E6%88%B3"><span class="toc-text">获取时间戳</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%97%B6%E9%97%B4%E6%88%B3%E7%9A%84%E5%AE%9A%E4%B9%89%E5%92%8C%E4%BD%9C%E7%94%A8"><span class="toc-text">时间戳的定义和作用</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#getTime-%EF%BC%9A%E8%8E%B7%E5%8F%96%E6%97%B6%E9%97%B4%E6%88%B3"><span class="toc-text">getTime()：获取时间戳</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%8E%B7%E5%8F%96-Date-%E5%AF%B9%E8%B1%A1%E7%9A%84%E6%97%B6%E9%97%B4%E6%88%B3"><span class="toc-text">获取 Date 对象的时间戳</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%8E%B7%E5%8F%96%E5%BD%93%E5%89%8D%E6%97%B6%E9%97%B4%E7%9A%84%E6%97%B6%E9%97%B4%E6%88%B3"><span class="toc-text">获取当前时间的时间戳</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%88%A9%E7%94%A8%E6%97%B6%E9%97%B4%E6%88%B3%E6%A3%80%E6%B5%8B%E4%BB%A3%E7%A0%81%E7%9A%84%E6%89%A7%E8%A1%8C%E6%97%B6%E9%97%B4"><span class="toc-text">利用时间戳检测代码的执行时间</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#format"><span class="toc-text">format()</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%BB%83%E4%B9%A0"><span class="toc-text">练习</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%B8%BE%E4%BE%8B1%EF%BC%9A%E6%A8%A1%E6%8B%9F%E6%97%A5%E5%8E%86"><span class="toc-text">举例1：模拟日历</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%B8%BE%E4%BE%8B2%EF%BC%9A%E5%8F%91%E5%B8%83%E4%BC%9A%E5%80%92%E8%AE%A1%E6%97%B6"><span class="toc-text">举例2：发布会倒计时</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Moment-js"><span class="toc-text">Moment.js</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%B5%9E%E8%B5%8F%E4%BD%9C%E8%80%85"><span class="toc-text">赞赏作者</span></a></li></ol>
    </article>
  
  
  <article class="card card-content">
    <div class="recent-posts-card">
  <div class="recent-posts-header">
    <i 
      class="iconfont icon-wenzhang_huaban" 
      style="padding-right: 2px;">
    </i>最近文章
  </div>
  <div class="recent-posts-list">
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2023-03-09</div>
        <a href="/2023/03/09/13-React%E5%9F%BA%E7%A1%80/11-React%20Navive%E5%88%9D%E8%AF%86/"><div class="recent-posts-item-content">11-React Navive初识</div></a>
      </div>
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2023-03-09</div>
        <a href="/2023/03/09/13-React%E5%9F%BA%E7%A1%80/10-AntD%E6%A1%86%E6%9E%B6%E7%9A%84upload%E7%BB%84%E4%BB%B6%E4%B8%8A%E4%BC%A0%E5%9B%BE%E7%89%87%E6%97%B6%E4%BD%BF%E7%94%A8customRequest%E6%96%B9%E6%B3%95%E8%87%AA%E5%AE%9A%E4%B9%89%E4%B8%8A%E4%BC%A0%E8%A1%8C%E4%B8%BA/"><div class="recent-posts-item-content">10-AntD框架的upload组件上传图片时使用customRequest方法自定义上传行为</div></a>
      </div>
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2023-03-09</div>
        <a href="/2023/03/09/13-React%E5%9F%BA%E7%A1%80/09-AntD%E6%A1%86%E6%9E%B6%E7%9A%84upload%E7%BB%84%E4%BB%B6%E4%B8%8A%E4%BC%A0%E5%9B%BE%E7%89%87%E6%97%B6%E9%81%87%E5%88%B0%E7%9A%84%E4%B8%80%E4%BA%9B%E5%9D%91/"><div class="recent-posts-item-content">09-AntD框架的upload组件上传图片时遇到的一些坑</div></a>
      </div>
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2023-03-09</div>
        <a href="/2023/03/09/13-React%E5%9F%BA%E7%A1%80/08-Ant%20Design%E7%9A%84%E5%9F%BA%E6%9C%AC%E4%BD%BF%E7%94%A8/"><div class="recent-posts-item-content">08-Ant Design的基本使用</div></a>
      </div>
    
  </div>
</div>
  </article>
  
  
</div>
            </aside>
          </div>
        </div>
      </div>
    </div>
     
    <footer class="footer">
  <div class="footer-container">
    <div>
      <div class="footer-dsc">
        <span>
          Copyright ©
          
            2023
          
          
        </span>
        &nbsp;
        <a 
          href="/" 
          class="footer-link">
          前端面试
        </a>
      </div>
    </div>

    
      <div class="footer-dsc">
        
          Powered by
          <a 
            href="https://hexo.io/" 
            class="footer-link" 
            target="_blank" 
            rel="nofollow noopener noreferrer">
            &nbsp;Hexo
          </a>
        
        
          <span>&nbsp;|&nbsp;</span>
        
        
          Theme -
          <a 
            href="https://github.com/theme-kaze" 
            class="footer-link" 
            target="_blank"
            rel="nofollow noopener noreferrer">
            &nbsp;Kaze
          </a>
        
      </div>
    
    
    
    
</footer>
 
    
  <a 
    role="button" 
    id="scrollbutton" 
    class="basebutton" 
    aria-label="回到顶部">
    <i class="iconfont icon-arrowleft button-icon"></i>
  </a>

<a 
  role="button" 
  id="menubutton"
  aria-label="menu button"
  class="basebutton">
  <i class="iconfont icon-menu button-icon"></i>
</a>
<a 
  role="button" 
  id="popbutton" 
  class="basebutton" 
  aria-label="控制中心">
  <i class="iconfont icon-expand button-icon"></i>
</a>
<a 
  role="button" 
  id="darkbutton" 
  class="basebutton darkwidget" 
  aria-label="夜色模式">
  <i class="iconfont icon-weather button-icon"></i>
</a>
<a 
  role="button" 
  id="searchbutton" 
  class="basebutton searchwidget" 
  aria-label="搜索">
  <i class="iconfont icon-search button-icon"></i>
</a> 
     
     
     
      <script>
  var addImgLayout = function () {
    var img = document.querySelectorAll('.post-content img')
    var i
    for (i = 0; i < img.length; i++) {
      var wrapper = document.createElement('a')
      wrapper.setAttribute('href', img[i].getAttribute('data-src'))
      wrapper.setAttribute('aria-label', 'illustration')
      wrapper.style.cssText =
        'width: 100%; display: flex; justify-content: center;'
      if (img[i].alt) wrapper.dataset.caption = img[i].alt
      wrapper.dataset.nolink = true
      img[i].before(wrapper)
      wrapper.append(img[i])
      var divWrap = document.createElement('div')
      divWrap.classList.add('gallery')
      wrapper.before(divWrap)
      divWrap.append(wrapper)
    }
    baguetteBox.run('.gallery')
  }
</script>
<script>
  loadScript(
    "/js/lib/lightbox/baguetteBox.min.js",
    addImgLayout
  )
</script>
 
     
     
    <script src="/js/main.js"></script> 
     
    
      <script>
        var addLazyload = function () {
          var observer = lozad('.lozad', {
            load: function (el) {
              el.srcset = el.getAttribute('data-src')
            },
            loaded: function (el) {
              el.classList.add('loaded')
            },
          })
          observer.observe()
        }
      </script>
      <script>
        loadScript('/js/lib/lozad.min.js', addLazyload)
      </script>
    
    <script src="//instant.page/5.1.0" type="module"
      integrity="sha384-by67kQnR+pyfy8yWP4kPO12fHKRLHZPfEsiSXR8u2IKcTdxD805MGUXBzVPnkLHw"></script>
    
    
  </body>
</html>
